package com.lixiaoyi.emp.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javafx.beans.property.Property;

import javax.sql.DataSource;
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


/**
 * @Author 李小毅
 * @Description
 * @Date 2022-05-11
 */

public class DbUtils {

    //获取连接池
    private static DruidDataSource ds;
    //线程的副本 保证同一个线程 获取到的conn是一个
    private static ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();


    public  static DataSource getDs() {
        return  ds;
    }

    //代码块
    static {

            //1.创建property对象
            Properties properties = new Properties();
            //2.字节流
            //InputStream is = new FileInputStream("/database.propertues");
            InputStream is = DbUtils.class.getResourceAsStream("/database.properties");
            //3.转换流
            //Reader reader = new InputStreamReader(is,"utf-8");

        try {
            //4.加载property
            properties.load(is);
            //5.初始化ds
            ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 创建连接
     * @return
     */
    public static Connection getConnection(){
        //1.从当前副本中获取
        Connection connection = THREAD_LOCAL.get();
        //2.如果空
        try {
            if (connection == null){
                //重新连接
                connection = ds.getConnection();
                //创建成功后 放入线程副本中
                THREAD_LOCAL.set(connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;


    }

    /**
     * 开启事务
     * @return
     */
    public static void begin(){

        //1.获取connection
        Connection connection = null;
        try {
            connection = getConnection();
            //2.关闭自动提交
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 提交
     */
    public static void commit(){
        //获取connection
        Connection connection = null;

        try {
            connection = getConnection();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(connection,null,null);
        }

    }

    /**
     * 回滚
     */
    public static void rollback(){
        Connection connection = null;

        try {
            connection = getConnection();
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(connection,null,null);
        }
    }

    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){

        try {
            if (resultSet!=null){
                resultSet.close();
            }
            if (statement!=null){
                statement.close();
            }
            if (connection!=null){
                connection.close();
                //从线程副本中移除
                THREAD_LOCAL.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
